package com.chenxin.interact.api;

import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.DeviceResult;
import com.chenxin.interact.domain.dto.MovePositionDTO;
import com.chenxin.interact.domain.dto.SmtSaveSuccessDTO;
import com.chenxin.interact.domain.dto.SmtTakeSuccessDTO;
import com.chenxin.interact.domain.dto.StopOutputBusinessDTO;
import com.chenxin.interact.service.ISmtToWmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("PassionStore/WCF_ApiControl")
public class SmtAPI extends BaseController {

    @Autowired
    private ISmtToWmsService smtToWmsService;

//    @Log(title = "智能仓入库成功接口", businessType = BusinessType.INSERT)
    @PostMapping("/SaveSuccess")
    public DeviceResult saveSuccess(@RequestBody SmtSaveSuccessDTO dto) {
        if (dto.getReelId() == null) {
            return DeviceResult.error("ReelId不能为空");
        }
        if (dto.getPN() == null){
            return DeviceResult.error("PN不能为空");
        }
        if (dto.getQty() <= 0) {
            return DeviceResult.error("数量不能小于0");
        }
        if (dto.getTowerNo() == null) {
            return DeviceResult.error("TowerNo不能为空");
        }
        if (dto.getSide() == null) {
            return DeviceResult.error("Side不能为空");
        }
        if (dto.getPositionCode() == null) {
            return DeviceResult.error("PositionCode不能为空");
        }
        dto.setUserId(103L);
        try {
            smtToWmsService.saveSuccess(dto);
            return DeviceResult.success("success");
        } catch (Exception e) {
            log.error("操作失败！" + e.getMessage());
            return DeviceResult.error("操作失败！" + e.getMessage());
        }
    }

//    @Log(title = "智能仓出库成功接口", businessType = BusinessType.INSERT)
    @PostMapping("/TakeSuccess")
    public DeviceResult taskSuccess(@RequestBody SmtTakeSuccessDTO dto) {
        if (dto.getReelId() == null) {
            return DeviceResult.error("ReelId不能为空");
        }
        if (dto.getPN() == null){
            return DeviceResult.error("PN不能为空");
        }
        if (dto.getQty() <= 0) {
            return DeviceResult.error("数量不能小于0");
        }
        if (dto.getTowerNo() == null) {
            return DeviceResult.error("TowerNo不能为空");
        }
//        if (dto.getSide() == null) {
//            return DeviceResult.error("Side不能为空");
//        }
        if (dto.getPositionCode() == null) {
            return DeviceResult.error("PositionCode不能为空");
        }
        if (dto.getOutType() != 0 && dto.getOutType() != 1) {
            return DeviceResult.error("出库方式只能为0或1");
        }
        if (dto.getType() == null){
            return DeviceResult.error("出库单类型不能为空");
        }
        if (dto.getOrderNo() == null){
            return DeviceResult.error("出库单号不能为空");
        }
//        if (dto.getCreator() == null){
//            return DeviceResult.error("创建人不能为空");
//        }
        dto.setUserId(103L);
        try {
            smtToWmsService.takeSuccess(dto);
            return DeviceResult.success("success");
        } catch (Exception e) {
            log.error("操作失败！" + e.getMessage());
            return DeviceResult.error("操作失败！" + e.getMessage());
        }
    }

//    @InterfaceLog(name = "智能仓请求入库接口")
    @PostMapping("/AllowInputWarehouse")
    public DeviceResult allowInputWarehouse(@RequestBody SmtSaveSuccessDTO dto) {
//        if (dto.getPN() == null){
//            return DeviceResult.error("PN不能为空");
//        }
        if (dto.getQty() <= 0) {
            return DeviceResult.error("数量不能小于0");
        }
        if (dto.getTowerNo() == null) {
            return DeviceResult.error("TowerNo不能为空");
        }
        dto.setUserId(103L);
        return smtToWmsService.allowInputWarehouse(dto);
    }

//    @Log(title = "智能仓锁定物料接口", businessType = BusinessType.UPDATE)
    @PostMapping("/LockPosition")
    public DeviceResult lockPosition(@RequestBody SmtSaveSuccessDTO dto) {
        if (dto.getReelId() == null) {
            return DeviceResult.error("ReelId不能为空");
        }
        if (dto.getTowerNo() == null) {
            return DeviceResult.error("TowerNo不能为空");
        }
        dto.setUserId(103L);
        return smtToWmsService.lockPosition(dto);
    }

//    @Log(title = "智能仓解锁物料接口", businessType = BusinessType.INSERT)
    @PostMapping("/UnLockPosition")
    public DeviceResult UnLockPosition(@RequestBody SmtSaveSuccessDTO dto) {
        if (dto.getReelId() == null) {
            return DeviceResult.error("ReelId不能为空");
        }
        if (dto.getTowerNo() == null) {
            return DeviceResult.error("TowerNo不能为空");
        }
        dto.setUserId(103L);
        return smtToWmsService.UnLockPosition(dto);
    }

//    @Log(title = "智能仓异常出库接口", businessType = BusinessType.INSERT)
    @PostMapping("/ErrorOut")
    public DeviceResult errorOut(@RequestBody SmtSaveSuccessDTO dto) {
        if (dto.getReelId() == null) {
            return DeviceResult.error("ReelId不能为空");
        }
        if (dto.getTowerNo() == null) {
            return DeviceResult.error("TowerNo不能为空");
        }
        dto.setUserId(103L);
        return smtToWmsService.errorOut(dto);
    }

//    @Log(title = "智能仓删除工单接口", businessType = BusinessType.INSERT)
    @PostMapping("/StopOutputBusiness")
    public DeviceResult stopOutputBusiness(@RequestBody StopOutputBusinessDTO dto) {
        if (dto.getWorkOrderId() == null) {
            return DeviceResult.error("工单号不能为空");
        }
        dto.setUserId(103L);
        return smtToWmsService.stopOutputBusiness(dto);
    }

    @PostMapping("/MovePosition")
    public DeviceResult movePosition(@RequestBody MovePositionDTO dto) {
        if (dto.getReelId() == null) {
            return DeviceResult.error("ReelId不能为空");
        }
        if (dto.getTransferType() == null) {
            return DeviceResult.error("中转方向不能为空");
        }
        if (dto.getPositionCode() == null) {
            return DeviceResult.error("库位编码不能为空");
        }
        dto.setUserId(103L);
        return smtToWmsService.movePosition(dto);
    }
}
